package com.blast.manager.delay.ui

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.KeyEvent
import androidx.recyclerview.widget.LinearLayoutManager
import com.blast.manager.R
import com.blast.manager.delay.adapter.DelayAdapter
import com.lxj.xpopup.XPopup
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.event.RefreshDataEvent
import com.wxl.common.life.LifecycleManager
import com.wxl.common.room.entity.LeiGuanEntity
import com.wxl.common.room.helper.DBHelper
import com.wxl.common.util.LayoutHelper
import com.wxl.common.util.ObservableUtil
import com.wxl.common.util.Toll
import io.reactivex.ObservableOnSubscribe
import kotlinx.android.synthetic.main.activity_delay_repeat_layout.*

class DelayRepeatActivity : AbsActivity() {


    companion object {
        fun start(context: Context, data:ArrayList<LeiGuanEntity>,  mRepeatData: ArrayList<LeiGuanEntity>){
            val intent = Intent(context, DelayRepeatActivity::class.java)
            AppContext.appContext.setLeiGuanEntity("repeatOldData",data)
            AppContext.appContext.setLeiGuanEntity("repeat",mRepeatData)
            context.startActivity(intent)
        }
    }

    private var mDelayAdapter: DelayAdapter? = null
    private var mRepeatData: ArrayList<LeiGuanEntity>? = null
    private var mData: ArrayList<LeiGuanEntity>? = null
    private var isCovering = false

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_delay_repeat_layout
    }

    override fun onCreateViewChanged() {
        isCheckAppUpgrade = false
        setTopTitle("延时方案重复")
        mRepeatData = AppContext.appContext.getLeiGuanEntity("repeat")
        mData = AppContext.appContext.getLeiGuanEntity("repeatOldData")
        mDelayAdapter = DelayAdapter(mRepeatData)
        LayoutHelper.getDetonatorTitleViewsWidth(delayRepeatTitleLayout,object : ObservableUtil.Companion.OnNextCallback<ArrayList<Int>>(){
            override fun onNext(t: ArrayList<Int>) {
                mDelayAdapter?.setWidths(t)
                delayRepeatListView.layoutManager = LinearLayoutManager(this@DelayRepeatActivity)
                mDelayAdapter?.bindToRecyclerView(delayRepeatListView)
            }
        })

        delayRepeatCancelBtn.setOnClickListener {
            if(isCovering){
                return@setOnClickListener
            }
            finish()
        }
        delayRepeatCoverBtn.setOnClickListener {
            if(isCovering){
                return@setOnClickListener
            }
            cover()
        }
    }

    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        mDelayAdapter?.onKeyDown(keyCode, event)
        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_F1) {
            delayRepeatCancelBtn.performClick()
        }
        if (keyCode == KeyEvent.KEYCODE_F4) {
            delayRepeatCoverBtn.performClick()
        }
        return true
    }

    private fun cover(){
        isCovering = true
        val popupView = XPopup.Builder(this)
            .dismissOnBackPressed(false)
            .dismissOnTouchOutside(false)
            .asLoading("正在覆盖...")
            .show()
        addPop(popupView)
        ObservableUtil.doNext(ObservableOnSubscribe<Boolean> {
            mRepeatData?.forEach { rd->
                mData?.forEach {
                    if(TextUtils.equals(it.UID,rd.UID)){
                        rd.password = it.password
                    }
                }
            }
            it.onNext(true)
        },object : ObservableUtil.Companion.OnNextCallback<Boolean>(){
            override fun onNext(t: Boolean) {
                DBHelper.updateLeiGuan(object : ObservableUtil.Companion.OnNextCallback<Boolean>(){
                    override fun onNext(t: Boolean) {
                        popupView.dismiss()
                        isCovering = false
                        Toll.toll("覆盖完成")
                        val mRefreshDataEvent =
                            RefreshDataEvent()
                        mRefreshDataEvent.isRefresh = true
                        LifecycleManager.manager.refreshLiveData(mRefreshDataEvent)
                        finish()
                    }
                },*mRepeatData!!.toTypedArray())
            }
        })
    }
}